package models

import (
	"car_srv/appconfig"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func Init() {
	var err error
	mysqlCong := appconfig.AppCong.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlCong.User, mysqlCong.Password, mysqlCong.Host, mysqlCong.Port, mysqlCong.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("mysql 连接配置失败")
	}
	logx.Info("mysql 连接配置成功")

	if err = DB.AutoMigrate(&User{}, &Good{}); err != nil {
		panic("数据表迁移失败")
	}
	logx.Info("数据表迁移成功")
}
